package com.aquarius.chapter2;

/**
 * 当类的属性或者说是成员参数越来越多时，构造器的使用会使程序可读性大大下降 使用内部静态类Builder的方式
 * 既可以在设置每个类的对象自己的所需的属性 又避免了类无法成为不可变类型的弊端。
 * @author aquarius
 *
 */
public class BetterPerson {

	private final int id;

	private String name;

	private String sex;

	private String address;

	private BetterPerson(Builder builder) {
		this.id = builder.inter_id;
		this.name = builder.inter_name;
		this.sex = builder.inter_sex;
		this.address = builder.inter_address;
	}

	public static class Builder {

		private int inter_id = 0;

		private String inter_name = null;

		private String inter_sex = null;

		private String inter_address = null;

		public Builder() {

		}

		public Builder setId(int id) {
			this.inter_id = id;
			return this;
		}

		public Builder setName(String name) {
			this.inter_name = name;
			return this;
		}

		public Builder setSex(String sex) {
			this.inter_sex = sex;
			return this;
		}

		public Builder setAddress(String address) {
			this.inter_address = address;
			return this;
		}

		public BetterPerson create() {
			return new BetterPerson(this);
		}
	}

	@Override
	public String toString() {
		return "BettterPerson [id=" + id + ", name=" + name + ", sex=" + sex + ", address=" + address + "]";
	}

}
